import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/login/Login.vue'
import Register from '../views/login/Register.vue'
import Article from '../views/article/Article.vue'
import ArticleList from '../views/article/ArticleList.vue'
import Books from '../components/books/Books.vue'
import Booksadd from '../components/books/Booksadd.vue'
import Booksaddlst from '../components/books/Bookslst.vue'
import Cate from '../components/cate/Cate.vue'
import Cateupdate from '../components/cate/Cateupdate.vue'
import Comment from '../components/comment/Comment.vue'
import Adminuser from '../components/adminuser/Adminuser.vue'
import Adminupdate from '../components/adminuser/Adminupdate.vue'
import User from '../components/user/User.vue'
import Userupdate from '../components/user/Userupdate.vue'
import Welcome from '../components/Welcome.vue'
import AdminLogin from '../components/admin/AdminLogin.vue'
import AdminIndex from '../components/admin/Index.vue'
import  '../plugins/element.js'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',//前台首页
    name: 'Home',
    component: Home
  },
  {
    path:'/login',//前台登录
    component:Login
  },
  {
    path:'/register',//前台注册 
    component:Register
  },
  {
    path:'/adminlogin',//后台登录路由
    component:AdminLogin
  },
  {
    path:'/admin',//后台首页路由
    component:AdminIndex,
    redirect:'/admin/welcome',
    children:[
      {path:'/admin/welcome',component:Welcome},
      {path:'/admin/books',component:Books},
      {path:'/admin/user',component:User},
      {path:'/admin/adminuser',component:Adminuser},
      {path:'/admin/cate',component:Cate},
      {path:'/admin/comment',component:Comment},
      {path:'/admin/books/add',component:Booksadd},
      {path:'/admin/books/addlst',component:Booksaddlst},
      {path:'/admin/adminupdate',component:Adminupdate},
      {path:'/admin/userupdate',component:Userupdate},
      {path:'/admin/cateupdate',component:Cateupdate},

    ]
  },
  {
    path:'/article',//书籍详情
    component:Article
  },
  {
    path:'/article/list',//书籍详情
    component:ArticleList
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/adminlogin') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/adminlogin')
  next()
})
export default router
